{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 含噪声量子线路\n",
    "\n",
    "[![下载Notebook](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_notebook.png)](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/master/mindquantum/zh_cn/mindspore_noise.ipynb)&emsp;\n",
    "[![下载样例代码](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_download_code.png)](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/notebook/master/mindquantum/zh_cn/mindspore_noise.py)&emsp;\n",
    "[![查看源文件](https://mindspore-website.obs.cn-north-4.myhuaweicloud.com/website-images/master/resource/_static/logo_source.png)](https://gitee.com/mindspore/docs/blob/master/docs/mindquantum/docs/source_zh_cn/noise.ipynb)\n",
    "\n",
    "## 概述\n",
    "\n",
    "在真实量子设备中，由于现阶段技术的不成熟，量子系统会由于量子操作、环境影响等各种因素而产生噪声。现阶段噪声对量子设备的影响不可忽略，因此在量子线路中加入对噪声的模拟，可以帮助我们寻找对噪声具有鲁棒性的量子算法，或设计减少噪声影响的纠错方案。\n",
    "\n",
    "噪声分为两种类型：相干噪声和非相干噪声。相干噪声一般来源于门操作中参数的噪声化，因此是幺正演化并且易于模拟；非相干噪声则来源于系统与环境的相互作用，因此通常是非幺正演化，会使量子系统从纯态变为混态，这个过程也被称为量子信道（Quantum channel）。混态的量子系统通常用密度矩阵的形式表示，但也可以通过“采样-统计”的蒙特卡洛法得到统计模拟结果。在MindQuantum中，我们用蒙特卡洛法来模拟量子信道，其中噪声门会以一定概率影响量子比特，通过对线路的多次采样，可以得到含噪声量子线路的模拟运行结果。\n",
    "\n",
    "量子信道的数学表示如下：\n",
    "\n",
    "$$\n",
    "\\varepsilon(\\rho) =\\sum_{k=0}^{m-1} E_{k} \\rho E_{k}^{\\dagger}\n",
    "$$\n",
    "\n",
    "其中$\\{E_k\\}$是Kraus算符，需要满足完备性条件$\\sum_k E_k^\\dagger E_k= I$；$\\rho$是量子态的密度矩阵，$\\varepsilon(\\rho)$是演化后的量子态。\n",
    "\n",
    "## 泡利信道（Pauli Channel）\n",
    "\n",
    "有一类噪声对量子线路的影响体现为，当进行了某种量子操作后，线路有概率额外受到一个泡利门的影响，被称为泡利信道。例如，经过某个操作后比特以一定概率被额外作用一个`X`门，即该比特以一定概率发生了翻转，这就是比特翻转信道(Bit Flip Channel)，数学表示为：\n",
    "\n",
    "$$\n",
    "\\varepsilon_{BF}( \\rho ) =(1-p)I \\rho I +pX \\rho X\n",
    "$$\n",
    "\n",
    "其中$I$是单位矩阵，$X$是泡利`X`门。该信道对应的Kraus算符为：\n",
    "\n",
    "$$\n",
    "E_0=\\sqrt{1-p}\\begin{bmatrix}\n",
    "1 & 0\\\\\n",
    "0 & 1\n",
    "\\end{bmatrix}, E_{1} =\\sqrt{p}\\begin{bmatrix}\n",
    "0 & 1\\\\\n",
    "1 & 0\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "其中$p$可以视为发生比特翻转的概率。由这个例子可以看到，一个纯态量子态经过比特翻转信道后会变为混态，该混态由原本的态和比特翻转后的态构成。\n",
    "\n",
    "同理，若比特以一定概率额外作用一个`Z`门称为相位翻转信道，`Y`门是比特相位翻转信道。若是`X`、`Y`、`Z`三个门平分概率，则称为去极化信道，该信道常常被用来描述真实量子硬件中门操作产生的噪声。此外，也可以使用通用的泡利信道自定义三个泡利门的概率。\n",
    "\n",
    "下面是这些信道的数学表示。\n",
    "\n",
    "泡利信道（Pauli Channel）：\n",
    "\n",
    "$$\n",
    "\\varepsilon _{Pauli}( \\rho ) =(1-p_x-p_y-p_z)\\rho + p_x X \\rho X+ p_y Y \\rho Y+p_z Z \\rho Z\n",
    "$$\n",
    "\n",
    "相位翻转信道（Phase Flip Channel）：\n",
    "\n",
    "$$\n",
    "\\varepsilon _{PF}( \\rho ) =(1-p)\\rho +pZ \\rho Z\n",
    "$$\n",
    "\n",
    "比特相位翻转信道（Bit-Phase Flip Channel）：\n",
    "\n",
    "$$\n",
    "\\varepsilon _{BPF}( \\rho ) =(1-p)\\rho +pY \\rho Y\n",
    "$$\n",
    "\n",
    "去极化信道（Depolarizing Channel）：\n",
    "\n",
    "$$\n",
    "\\varepsilon _{DF}( \\rho ) =(1-p)\\rho + \\frac{p}{3}(X \\rho X+ Y \\rho Y+Z \\rho Z)\n",
    "$$\n",
    "\n",
    "下面我们以去极化信道为例，看看如何将噪声信道加入线路中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 615,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"276.8\" height=\"80\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"276.8\" height=\"80\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<line x1=\"48.8\" x2=\"256.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nX\n </text>\n\n\n<rect x=\"132.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"152.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nDC\n </text>\n\n<rect x=\"192.8\" y=\"20.0\" width=\"40\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#ff7272\" fill-opacity=\"1\" />\n<circle cx=\"212.8\" cy=\"50.4\" r=\"1.6\" fill=\"#ffffff\" />\n<path d=\"M 200.0 50.4 A 12.8 12.8 0 0 1 225.60000000000002 50.4\" stroke=\"#ffffff\" stroke-width=\"2.4000000000000004\" fill-opacity=\"0\" />\n<path d=\"M 216.90184831748593 33.93539030917347 L 225.21569219381655 29.135390309173467 L 225.21569219381655 38.73539030917347 L 221.8901546432843 36.815390309173466 L 214.04707658144957 50.4 L 212.38430780618347 49.44 L 220.2273858680182 35.85539030917347 Z\" fill=\"#ffffff\" />\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x1592c2a60>"
      ]
     },
     "execution_count": 615,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.core.gates import DepolarizingChannel, X\n",
    "from mindquantum.core.circuit import Circuit\n",
    "\n",
    "circ = Circuit()\n",
    "circ += X.on(0)\n",
    "circ += DepolarizingChannel(0.1).on(0)\n",
    "circ.measure(0)\n",
    "circ.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "此时我们成功搭建了一个单比特的量子线路，该比特在作用`X`门后，会受到去极化噪声的影响，具体表现为有10%概率额外作用一个泡利门，其中`X`、`Y`、`Z`门平分概率。\n",
    "\n",
    "现在我们对该线路进行1000次模拟并输出采样结果："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 616,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"401.2\" height=\"147.0\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"401.2\" height=\"147.0\" fill=\"#ffffff\" />\n<text x=\"10\" y=\"17.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nShots:\n 1000\n </text>\n<text x=\"10\" y=\"31.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" >\nKeys: q0\n </text>\n<line x1=\"31.2\" x2=\"391.2\" y1=\"62.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"31.2\" x2=\"31.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"33.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.0\n </text>\n<line x1=\"31.2\" x2=\"31.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<line x1=\"91.2\" x2=\"91.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"93.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.186\n </text>\n<line x1=\"91.2\" x2=\"91.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"151.2\" x2=\"151.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"153.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.372\n </text>\n<line x1=\"151.2\" x2=\"151.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"211.2\" x2=\"211.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"213.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.558\n </text>\n<line x1=\"211.2\" x2=\"211.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"271.2\" x2=\"271.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"273.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.744\n </text>\n<line x1=\"271.2\" x2=\"271.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<line x1=\"331.2\" x2=\"331.2\" y1=\"55.0\" y2=\"62.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<text x=\"333.2\" y=\"60.0\" font-size=\"12px\" dominant-baseline=\"bottom\" text-anchor=\"start\" fill=\"#575d6c\" >\n0.93\n </text>\n<line x1=\"331.2\" x2=\"331.2\" y1=\"62.0\" y2=\"137.0\" stroke=\"#dfe1e6\" stroke-width=\"1\" />\n<text x=\"22.2\" y=\"85.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n0\n </text>\n<line x1=\"24.2\" x2=\"31.2\" y1=\"85.0\" y2=\"85.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"31.2\" y=\"73.0\" width=\"22.58064516129032\" height=\"24\" id=\"bar_0_1652788863999537000\" fill=\"#5e7ce0\" />\n<text x=\"63.78064516129032\" y=\"85.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_0_1652788863999560000\" fill-opacity=\"0\" >\n70\n </text>\n<text x=\"22.2\" y=\"115.0\" font-size=\"12px\" dominant-baseline=\"middle\" text-anchor=\"end\" fill=\"#575d6c\" >\n1\n </text>\n<line x1=\"24.2\" x2=\"31.2\" y1=\"115.0\" y2=\"115.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n<rect x=\"31.2\" y=\"103.0\" width=\"300.0\" height=\"24\" id=\"bar_1_1652788863999582000\" fill=\"#16acff\" />\n<text x=\"341.2\" y=\"115.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"start\" fill=\"#575d6c\" id=\"bar_text_1_1652788863999601000\" fill-opacity=\"0\" >\n930\n </text>\n<animate xlink:href=\"#bar_0_1652788863999537000\" attributeName=\"width\" from=\"0\" to=\"22.58064516129032\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 22.58064516129032\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_1_1652788863999582000\" attributeName=\"width\" from=\"0\" to=\"300.0\" dur=\"0.3s\" calcMode=\"spline\" values=\"0; 300.0\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" />\n<animate xlink:href=\"#bar_1_1652788863999582000\" attributeName=\"fill\" from=\"#16acff\" to=\"#fac209\" dur=\"0.15s\" calcMode=\"spline\" values=\"#16acff; #fac209\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_0_1652788863999560000\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<animate xlink:href=\"#bar_text_1_1652788863999601000\" attributeName=\"fill-opacity\" from=\"0\" to=\"1\" dur=\"0.15s\" calcMode=\"spline\" values=\"0; 1\" keyTimes=\"0; 1\" keySplines=\"0.42 0 1 0.8;\" fill=\"freeze\" begin=\"0.3s\" />\n<text x=\"203.1\" y=\"41.0\" font-size=\"14px\" dominant-baseline=\"middle\" text-anchor=\"middle\" >\nprobability\n </text>\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.measure_res_svg_drawer.SVGMeasure at 0x15933d3d0>"
      ]
     },
     "execution_count": 616,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.simulator import Simulator\n",
    "\n",
    "sim = Simulator('mqvector', 1)\n",
    "result = sim.sampling(circ, shots=1000)\n",
    "result.svg()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "倘若没有噪声影响，量子态应该处于$|1\\rangle$态，测量结果全为1，但在以上结果中，约有7%的模拟结果测得为0，这就是去极化信道产生的影响。采样结果与预期中3.3%概率额外作用`X`门、`Y`门和`Z`门的预测结果相符。\n",
    "\n",
    "至此我们就完成了一次对含去极化噪声量子线路的模拟。\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 阻尼信道（Damping Channel）\n",
    "\n",
    "振幅阻尼信道（Amplitude Damping Channel）和相位阻尼信道（Phase Damping Channel）也是比较常见的信道，其中振幅阻尼信道描述了量子系统能量的耗散，相位阻尼信道则描述了系统量子信息的损失。具体来说，振幅阻尼信道会使量子比特从激发态回到基态，相位阻尼信道则会使量子比特从叠加态退回到坍缩态。\n",
    "\n",
    "振幅阻尼信道的表达形式为：\n",
    "\n",
    "$$\n",
    "\\varepsilon_{AD}( \\rho ) =E_{0} \\rho E_{0}^{\\dagger } +E_{1} \\rho E_{1}^{\\dagger }\n",
    "$$\n",
    "\n",
    "其中$E_{0}, E_{1}$是Kraus算符，\n",
    "\n",
    "$$\n",
    "E_{0} =\\begin{bmatrix}\n",
    "1 & 0\\\\\n",
    "0 & \\sqrt{1-\\gamma }\n",
    "\\end{bmatrix}, E_{1} =\\begin{bmatrix}\n",
    "0 & \\sqrt{\\gamma }\\\\\n",
    "0 & 0\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "$\\gamma$为耗散系数。\n",
    "\n",
    "相位阻尼信道的表达形式为：\n",
    "\n",
    "$$\n",
    "\\varepsilon_{PD}( \\rho ) =E_{0} \\rho E_{0}^{\\dagger } +E_{1} \\rho E_{1}^{\\dagger }\n",
    "$$\n",
    "\n",
    "其中\n",
    "\n",
    "$$\n",
    "E_{0} =\\begin{bmatrix}\n",
    "1 & 0\\\\\n",
    "0 & \\sqrt{1-\\gamma }\n",
    "\\end{bmatrix}, E_{1} =\\begin{bmatrix}\n",
    "0 & 0\\\\\n",
    "0 & \\sqrt{\\gamma }\n",
    "\\end{bmatrix}\n",
    "$$\n",
    "\n",
    "$\\gamma$为损失系数。\n",
    "\n",
    "由以上两式可以发现，阻尼信道的Kraus算符不具有幺正性，因此阻尼信道不能表示为以一定概率作用量子门的形式。\n",
    "接下来我们来看看两个阻尼信道的效果，首先是振幅阻尼信道。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 617,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"216.8\" height=\"80\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"216.8\" height=\"80\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<line x1=\"48.8\" x2=\"196.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n\n<rect x=\"132.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"152.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nADC\n </text>\n\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x1594c8fd0>"
      ]
     },
     "execution_count": 617,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.core.gates import AmplitudeDampingChannel, H\n",
    "\n",
    "circ2 = Circuit()\n",
    "circ2 += H.on(0)\n",
    "circ2 += AmplitudeDampingChannel(0.8).on(0)\n",
    "circ2.svg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 618,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1¦0⟩\n",
      "\n",
      "0.912870929175277¦0⟩\n",
      "0.408248290463863¦1⟩\n",
      "\n",
      "0.912870929175277¦0⟩\n",
      "0.408248290463863¦1⟩\n",
      "\n",
      "1¦0⟩\n",
      "\n",
      "1¦0⟩\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 将线路演化5次并打印量子态\n",
    "for i in range(5):\n",
    "    sim.reset()                 # 初始化模拟器\n",
    "    sim.apply_circuit(circ2)    # 演化线路\n",
    "    print(sim.get_qs(ket=True)) # 获取量子态\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从以上采样结果可以看出，振幅阻尼信道作用在叠加态 $|\\psi\\rangle=|0\\rangle+|1\\rangle$态后会使其变为混态，混态的其中一部分是$|0\\rangle$态，另一部分仍是叠加态，但$|1\\rangle$态的振幅发生了衰减。该混态的比例由初始量子态和耗散系数$\\gamma$共同决定，初始量子态中的$|1\\rangle$态振幅越大，混态中$|0\\rangle$态的比例就越大。\n",
    "\n",
    "具体来说，对于初始量子态 $|\\psi\\rangle=a|0\\rangle+b|1\\rangle$，振幅阻尼信道作用后变成$|0\\rangle$态的概率为 $p=\\gamma b^2$，或以$(1-p)$的概率变为叠加态 $|\\psi'\\rangle=\\frac{1}{\\sqrt{1-\\gamma b^{2}}}(a|0\\rangle+b\\sqrt{1-\\gamma}|1\\rangle)$。\n",
    "\n",
    "由此可见，量子比特经过振幅阻尼信道后能量发生耗散，叠加态中激发态$|1\\rangle$态的部分减少，或者直接变为基态$|0\\rangle$态。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "接下来是相位阻尼信道。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 619,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": "<div class=\"nb-html-output output_area\"><svg xmlns=\"http://www.w3.org/2000/svg\" width=\"216.8\" height=\"80\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\n<rect x=\"0\" y=\"0\" width=\"216.8\" height=\"80\" fill=\"#ffffff\" />\n<text x=\"20.0\" y=\"40.0\" font-size=\"16px\" dominant-baseline=\"middle\" text-anchor=\"start\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#252b3a\" >\nq0:\n </text>\n<line x1=\"48.8\" x2=\"196.8\" y1=\"40.0\" y2=\"40.0\" stroke=\"#adb0b8\" stroke-width=\"1\" />\n\n<rect x=\"72.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"92.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nH\n </text>\n\n\n<rect x=\"132.8\" y=\"20.0\" width=\"40.0\" height=\"40\" rx=\"4\" ry=\"4\" stroke=\"#ffffff\" stroke-width=\"0\" fill=\"#5e7ce0\" fill-opacity=\"1\" />\n<text x=\"152.8\" y=\"40.0\" font-size=\"20px\" dominant-baseline=\"middle\" text-anchor=\"middle\" font-family=\"Arial\" font-weight=\"normal\" fill=\"#ffffff\" >\nPDC\n </text>\n\n</svg></div>",
      "text/plain": [
       "<mindquantum.io.display.circuit_svg_drawer.SVGCircuit at 0x1590599d0>"
      ]
     },
     "execution_count": 619,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from mindquantum.core.gates import PhaseDampingChannel\n",
    "\n",
    "circ3 = Circuit()\n",
    "circ3 += H.on(0)\n",
    "circ3 += PhaseDampingChannel(0.7).on(0)\n",
    "circ3.svg()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 620,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1¦1⟩\n",
      "\n",
      "0.8770580193070293¦0⟩\n",
      "0.4803844614152615¦1⟩\n",
      "\n",
      "0.8770580193070293¦0⟩\n",
      "0.4803844614152615¦1⟩\n",
      "\n",
      "0.8770580193070293¦0⟩\n",
      "0.4803844614152615¦1⟩\n",
      "\n",
      "1¦1⟩\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 将线路演化5次并打印量子态\n",
    "for i in range(5):\n",
    "    sim.reset()                 # 初始化模拟器\n",
    "    sim.apply_circuit(circ3)    # 演化线路\n",
    "    print(sim.get_qs(ket=True)) # 获取量子态\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "与振幅阻尼信道类似，相位阻尼信道作用在叠加态 $|\\psi\\rangle=|0\\rangle+|1\\rangle$态后同样会使其变为混态，混态的其中一部分是$|1\\rangle$态，另一部分仍是叠加态，但$|1\\rangle$态的振幅发生了衰减。该混态的比例由初始量子态和耗散系数$\\gamma$共同决定，初始量子态中的$|1\\rangle$态振幅越大，混态中$|1\\rangle$态的比例就越大。\n",
    "\n",
    "具体来说，对于初始量子态 $|\\psi\\rangle=a|0\\rangle+b|1\\rangle$，相位阻尼信道作用后变成$|1\\rangle$态的概率为 $p=\\gamma b^2$，或以$(1-p)$的概率变为叠加态 $|\\psi'\\rangle=\\frac{1}{\\sqrt{1-\\gamma b^{2}}}(a|0\\rangle+b\\sqrt{1-\\gamma}|1\\rangle)$。\n",
    "\n",
    "由此可见，量子比特经过相位阻尼信道后并没有损失能量，但量子信息（这里体现为量子态叠加）发生了损失。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 实例演示——含噪声QAOA\n",
    "\n",
    "量子近似优化算法（Quantum Approximate Optimization Algorithm）是利用量子计算机来近似解决组合优化问题的量子算法，详尽的介绍可以参考教案[quantum_approximate_optimization_algorithm.ipynb](https://mindspore.cn/mindquantum/docs/zh-CN/master/quantum_approximate_optimization_algorithm.html)，在此我们以该算法为案例，看看引入噪声会对量子算法产生怎样的影响。在这里我们与QAOA教案保持一致，解决同一个图的Max-Cut问题。\n",
    "\n",
    "由于目前模拟器采用蒙特卡洛法模拟噪声，每次演化线路的结果具有随机性，与真实量子计算机类似，但也因此需要多次采样并分析统计结果，即每次计算哈密顿量期望值之前都需要先重新演化线路。与此同时，我们也无法直接得到含噪声量子线路的梯度，因此需要采用非梯度的优化方法。Nelder-Mead法可以得到多元标量函数的最小值，这里我们采用它来优化含参线路，得到目标哈密顿量期望值的最小值。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 626,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "max cut: 4.626187448255814\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAb4AAAEuCAYAAADx63eqAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAAsTAAALEwEAmpwYAAA06UlEQVR4nO3dd1hUZ94+8HtoIiBCIiKCgIL0GWONJSoqoAGiJpZIHHvJXqZo2ruJ/jabpm96sokaXzXZ7Muo0RC7xhpF86pr20iRahSkCggiZWBmOL8/XGYlxy7MmZlzf65rr83CMN7kcrk5z/N8z1E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r7M0rveJuy21EogNFhni3m+IiITIFXfPTAXogMhKPdgz2Sx9HOFgsiA1s5ERHR3bH46IH16uaGJbEhaG9/f3+N2tvbYElsyD3droyIqLXxcAs9lOYbTS/dnQmt3iD50xmIiO6Ge3zUKlIKqrDycC4OZZVBAUB7i+fxjQj2wILIQF7pEZGkWHzUqipqGpB0tgCZxddRrdXB1dEeIV4dMLGP+T2BnYjkicVHRESywsMtREQkKyw+IiKSFRYfERHJCouPiIhkhcVHRESywuIjIiJZYfEREZGssPiIiEhWWHxERCQrLD4iIpIVFh8REckKi4+IiGSFxUdERLLC4iMiIllh8RERkayw+IiISFZYfEREJCssPiIikhUWHxERyQqLj4iIZIXFR0REsvL/AaorMKdxoMKMAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from mindquantum.core.circuit import Circuit, UN\n",
    "from mindquantum.core.gates import H, ZZ, RX, AmplitudeDampingChannel\n",
    "from mindquantum.core.operators import Hamiltonian, QubitOperator\n",
    "from mindquantum.simulator import Simulator\n",
    "from scipy.optimize import minimize\n",
    "import networkx as nx\n",
    "import mindspore as ms\n",
    "ms.set_context(mode=ms.PYNATIVE_MODE, device_target=\"CPU\")\n",
    "\n",
    "# 生成待求解的图\n",
    "g = nx.Graph()\n",
    "nx.add_path(g, [0, 1])\n",
    "nx.add_path(g, [1, 2])\n",
    "nx.add_path(g, [2, 3])\n",
    "nx.add_path(g, [3, 4])\n",
    "nx.add_path(g, [0, 4])\n",
    "nx.add_path(g, [0, 2])\n",
    "nx.draw(g, with_labels=True, font_weight='bold')\n",
    "\n",
    "gamma = 0.005                                        # 设定振幅阻尼信道的耗散系数\n",
    "circ = Circuit(UN(H, g.nodes))                       # 生成均匀叠加态\n",
    "circ += UN(AmplitudeDampingChannel(gamma), g.nodes)  # 假设每个H门作用后都会出现噪声\n",
    "\n",
    "# 生成ansatz线路，详细的原理请参考QAOA教案\n",
    "for i in range(4):\n",
    "    for j in g.edges:\n",
    "        circ += ZZ(f'g{i}').on(j)                    # 对图中的每条边作用ZZ门\n",
    "    circ.barrier(False)\n",
    "    for j in g.nodes:\n",
    "        circ += RX(f'b{i}').on(j)                    # 对每个节点作用RX门\n",
    "        circ += AmplitudeDampingChannel(gamma).on(j) # 假设每个RX门作用后都会出现噪声\n",
    "    circ.barrier(False)\n",
    "\n",
    "# 生成哈密顿量\n",
    "ham = QubitOperator()\n",
    "for i in g.edges:\n",
    "    ham += QubitOperator(f'Z{i[0]} Z{i[1]}')\n",
    "ham = Hamiltonian(ham)\n",
    "\n",
    "sim = Simulator('mqvector', circ.n_qubits)           # 初始化模拟器\n",
    "\n",
    "# 定义待优化的函数，由于噪声存在，每次需要重新演化线路\n",
    "def func(x):\n",
    "    sim.reset()                                      # 重置模拟器\n",
    "    sim.apply_circuit(circ, x)                       # 演化线路，参数x是优化器生成的线路参数数值\n",
    "    f = sim.get_expectation(ham).real                # 计算并返回哈密顿量的期望值\n",
    "    return f\n",
    "\n",
    "# 设置线路参数的初始数值\n",
    "n_paras = len(circ.params_name)\n",
    "init_amp = [0.0 for i in range(n_paras)]\n",
    "\n",
    "res = minimize(func, init_amp, method='nelder-mead') # 使用优化器优化参数，得到哈密顿量期望值的最小值，优化方法是nelder-mead\n",
    "cut = (len(g.edges) - res.fun) / 2                   # 计算max-cut数\n",
    "print('max cut:', cut)                               # 打印结果"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从结果可以看出，受到噪声影响，QAOA算法的结果变差，但当耗散系数不高时还是能收敛得到一个接近的结果。"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "8dbbe1bd74c2d2ca4a38c5ad94deb14cee554a4321ae2f6401344c296fed25fd"
  },
  "kernelspec": {
   "display_name": "MindSpore",
   "language": "python",
   "name": "mindspore"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
